﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace Nirvana
{
    [CustomEditor(typeof(TextureImportRule), true)]
    public class TextureImportRuleEditor : Editor
    {
        private SerializedProperty includeRegexs;
        private SerializedProperty excludeRegexs;


		private SerializedProperty textureType;
		private SerializedProperty textureShape;
		private SerializedProperty wrapMode;
		private SerializedProperty filterMode;
		private SerializedProperty anisoLevel;
		private SerializedProperty sRGBTexture;
		private SerializedProperty alphaSource;
		private SerializedProperty alphaIsTransparency;
        private SerializedProperty mipmapEnabled;
        private SerializedProperty isReadable;
        private SerializedProperty isFix4Rate;

        private SerializedProperty nonPowerOf2;
		private SerializedProperty streamingMipmaps;
		private SerializedProperty StreamingMipMapPriority;

        private SerializedProperty borderMipmap;
        private SerializedProperty mipmapFilter;
        private SerializedProperty mipMapBias;
        private SerializedProperty mipMapsPreserveCoverage;
        private SerializedProperty alphaCoutoffValue;
        private SerializedProperty fadeout;
        private SerializedProperty mipmapFadeDistanceStart;
        private SerializedProperty mipmapFadeDistanceEnd;
        private SerializedProperty allowAlphaSplitting;
        private SerializedProperty convertToNormalmap;
        private SerializedProperty normalmapFilter;
        private SerializedProperty heightmapScale;
        private SerializedProperty generateCubemap;

        private SerializedProperty androidTextureFormatNoAlpha;
		private SerializedProperty androidTextureFormatAlpha;
		private SerializedProperty iosTextureFormatNoAlpha, iosTextureFormatAlpha;
        private SerializedProperty webGLTextureFormat;
        private SerializedProperty maxTextureSize;
		private SerializedProperty compressionQuality;


		public override void OnInspectorGUI()
        {
            base.serializedObject.Update();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents(GUI.skin.window);
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n\n" +
                "正则例子：1.匹配非空字符：\\S , 2.任意字符：. ", MessageType.None);
            GUI.color = Color.white;
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.includeRegexs);
            EditorGUILayout.PropertyField(this.excludeRegexs);

            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            GUILayout.Label("Texture规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents(GUI.skin.window);
            EditorGUILayout.PrefixLabel("Common");
            EditorGUILayout.PropertyField(this.textureType);
            EditorGUILayout.PropertyField(this.textureShape);
            EditorGUILayout.PropertyField(this.sRGBTexture);
            EditorGUILayout.PropertyField(this.alphaSource);
            EditorGUILayout.PropertyField(this.alphaIsTransparency);

            EditorGUILayout.PropertyField(this.wrapMode);
            EditorGUILayout.PropertyField(this.filterMode);
            EditorGUILayout.PropertyField(this.anisoLevel);



            EditorGUILayout.Space();
            EditorGUILayout.PrefixLabel("Advanced");
            EditorGUILayout.PropertyField(this.nonPowerOf2);
            EditorGUILayout.PropertyField(this.isReadable);
            EditorGUILayout.PropertyField(this.isFix4Rate);
            EditorGUILayout.Space();


            EditorGUILayout.Space();
            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.HelpBox("流式Mip,按需动态加载Mip数据到内存，而不是全部加载\n" +
                "开启该功能的好处是可以节省关卡加载时间,通常这样的系统通过仅加载渲染场景中当前相机位置所需的mipmap来减少纹理所需的总内存量,它可以节省少量CPU成本，从而节省大量GPU内存。\n", MessageType.None);
            EditorGUILayout.PropertyField(this.streamingMipmaps);
            EditorGUILayout.PropertyField(this.StreamingMipMapPriority);
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();


            EditorGUILayout.Space();
            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("Mip Map");
            EditorGUILayout.PropertyField(this.mipmapEnabled);
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("启用此属性可避免颜色向外渗透到较低 MIP 级别的边缘。该功能用于光照剪影。默认情况下会禁用此属性。");
            EditorGUILayout.PropertyField(this.borderMipmap);
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("有两种 Mipmap 过滤方法可用于优化图像质量。默认选项为 Box。");
            EditorGUILayout.PropertyField(this.mipmapFilter);
            EditorGUILayout.Space();
            // GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.LabelField("纹理的多级渐进纹理偏差,正偏差会使纹理看起来更加模糊，负偏差会锐化 纹理。");
            EditorGUILayout.LabelField("注意，使用较大的负偏差会降低性能，因此不建议使用 超过 -0.5 的负偏差");
            EditorGUILayout.PropertyField(this.mipMapBias);
            // GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            EditorGUILayout.LabelField("如果希望生成的 Mipmap 的 Alpha 通道在 Alpha Test期间保留覆盖率,改善Cutout材质（亦即alpha tested材质）");
            EditorGUILayout.PropertyField(this.mipMapsPreserveCoverage);
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("设置用于在 Alpha Test期间控制 MipMap 覆盖范围的参考值。");
            EditorGUILayout.PropertyField(this.alphaCoutoffValue);
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("允许在相关平台上对此纹理进行 Alpha 通道分离。");
            EditorGUILayout.PropertyField(this.allowAlphaSplitting);
            EditorGUILayout.Space();


            EditorGUILayout.Space();
            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.LabelField("启用此属性可在 MIP 级别递进时使 Mipmap 淡化为灰色。此属性用于细节贴图。");
            EditorGUILayout.PropertyField(this.fadeout,new GUIContent("Fadeout Mip Maps"));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("第一个开始淡出的 MIP 级别"); 
            EditorGUILayout.PropertyField(this.mipmapFadeDistanceStart);
            EditorGUILayout.LabelField("纹理完全变灰的 MIP 级别。");
            EditorGUILayout.PropertyField(this.mipmapFadeDistanceEnd);
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            EditorGUILayout.Space();
            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("Normal Map");
            EditorGUILayout.LabelField("转换高度图为法线图，用于指定一些高度图，自动转换NormalMap格式.");
            EditorGUILayout.LabelField("比如：自动转化任何文件名中带有 _bumpmap 的纹理文件为法线贴图");
            EditorGUILayout.PropertyField(this.convertToNormalmap);
            EditorGUILayout.PropertyField(this.normalmapFilter);
            EditorGUILayout.PropertyField(this.heightmapScale);
            EditorGUILayout.PropertyField(this.generateCubemap);
            GUILayoutEx.EndContents();

            EditorGUILayout.Space();

            EditorGUILayout.Space();
            Color32 backgroundColor =  GUI.backgroundColor;
            GUILayoutEx.BeginContents(GUI.skin.box);
         
            GUI.backgroundColor = Color.yellow;
            EditorGUILayout.PrefixLabel("Android Texture Format");
            EditorGUILayout.HelpBox("针对Android平台Unity还实现了一套Crunched（紧缩）压缩方式，比如RGBA Crunched ETC2，先ETC2先压缩，然后在Crunched压缩，运行时在额外解压缩，所以大小更下，加载更快。\n" +
                "建议选择：ETC_RGB4 Crunched 和ETC_RGBA8 Crunched\n", MessageType.None);
            EditorGUILayout.PropertyField(this.androidTextureFormatNoAlpha,new GUIContent("NoAlpha"));
            EditorGUILayout.PropertyField(this.androidTextureFormatAlpha,new GUIContent("Alpha"));
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("IOS Texture Format");
            EditorGUILayout.HelpBox("从iOS9(A8架构)开始支持ASTC压缩格式 ，相对于PVRTC2/4而言，ASTC拥有更高的质量，且占用更少的内存。\n" +
            "建议选择：无Alpha的ASTC 6x6 ，带Alpha的ASTC 5x5,有更高要求的贴图（比如特效、UI），可以设置压缩格式为ASTC 4x4\n", MessageType.None);
            EditorGUILayout.PropertyField(this.iosTextureFormatNoAlpha, new GUIContent("NoAlpha"));
            EditorGUILayout.PropertyField(this.iosTextureFormatAlpha, new GUIContent("Alpha"));
            GUILayoutEx.EndContents();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("WebGL Texture Format");
            EditorGUILayout.HelpBox("从unity2021,webgl开始支持ASTC压缩格式 ，ASTC拥有更高的质量，且占用更少的内存。\n" , MessageType.None);
            EditorGUILayout.PropertyField(this.webGLTextureFormat, new GUIContent("WebGL"));
            GUILayoutEx.EndContents();
            
            GUI.backgroundColor = backgroundColor;

            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.maxTextureSize);
            EditorGUILayout.PropertyField(this.compressionQuality, new GUIContent("compressionQuality(品质0-100)"));
            EditorGUILayout.Space(10f);
         
            GUILayoutEx.EndContents();
            
            base.serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.includeRegexs = serializedObject.FindProperty("includeRegexs");
            this.excludeRegexs = serializedObject.FindProperty("excludeRegexs");

            this.wrapMode = serializedObject.FindProperty("wrapMode");
            this.filterMode = serializedObject.FindProperty("filterMode");
            this.anisoLevel = serializedObject.FindProperty("anisoLevel");
            this.sRGBTexture = serializedObject.FindProperty("sRGBTexture");
            this.alphaSource = serializedObject.FindProperty("alphaSource");
            this.alphaIsTransparency = serializedObject.FindProperty("alphaIsTransparency");
            this.isReadable = serializedObject.FindProperty("isReadable");
            this.isFix4Rate = serializedObject.FindProperty("isFix4Rate");
            
            this.mipmapEnabled = serializedObject.FindProperty("mipmapEnabled");
            this.androidTextureFormatNoAlpha = serializedObject.FindProperty("androidTextureFormatNoAlpha");
            this.androidTextureFormatAlpha = serializedObject.FindProperty("androidTextureFormatAlpha");
            this.iosTextureFormatNoAlpha = serializedObject.FindProperty("iosTextureFormatNoAlpha");
            this.iosTextureFormatAlpha = serializedObject.FindProperty("iosTextureFormatAlpha");
            this.webGLTextureFormat = serializedObject.FindProperty("webGLTextureFormat");
            
            this.maxTextureSize = serializedObject.FindProperty("maxTextureSize");
            this.compressionQuality = serializedObject.FindProperty("compressionQuality");

            this.textureType = serializedObject.FindProperty("textureType");
            this.textureShape = serializedObject.FindProperty("textureShape");
            this.nonPowerOf2 = serializedObject.FindProperty("nonPowerOf2");
            this.streamingMipmaps = serializedObject.FindProperty("streamingMipmaps");
            this.StreamingMipMapPriority = serializedObject.FindProperty("StreamingMipMapPriority");
            this.borderMipmap = serializedObject.FindProperty("borderMipmap");
            this.mipmapFilter = serializedObject.FindProperty("mipmapFilter");
            this.mipMapBias = serializedObject.FindProperty("mipMapBias");
            this.mipMapsPreserveCoverage = serializedObject.FindProperty("mipMapsPreserveCoverage");
            this.alphaCoutoffValue = serializedObject.FindProperty("alphaCoutoffValue");
            this.fadeout = serializedObject.FindProperty("fadeout");
            this.mipmapFadeDistanceStart = serializedObject.FindProperty("mipmapFadeDistanceStart");
            this.mipmapFadeDistanceEnd = serializedObject.FindProperty("mipmapFadeDistanceEnd");
            this.allowAlphaSplitting = serializedObject.FindProperty("allowAlphaSplitting");
            this.convertToNormalmap = serializedObject.FindProperty("convertToNormalmap");
            this.normalmapFilter = serializedObject.FindProperty("normalmapFilter");
            this.heightmapScale = serializedObject.FindProperty("heightmapScale");
            this.generateCubemap = serializedObject.FindProperty("generateCubemap");

        }


    }
}

